<?php

require_once APPPATH."/controllers/BasePriv.php";
require_once APPPATH."/libraries/phpqrcode/phpqrcode.php";
require_once APPPATH."/libraries/aws/aws-autoloader.php";
require_once APPPATH."/libraries/Tools.php";
use Aws\S3\S3Client;
class Statics extends BasePriv
{
    public function __construct()
    {
        parent::__construct();
        $this->load->helper(array('form', "url"));
        $this->load->model("Host_statics", "table");
    }

    /**
     * 前端基础数据
     */
    public function index(){
        $arr=[];
        if(!empty($_GET['date']) && $_GET['date']==1){
            $arr=$this->time(1);
        }else if(!empty($_GET['date']) && $_GET['date']==3){
            $arr=$this->time(3);
        }else if(!empty($_GET['date']) && $_GET['date']==7){
            $arr=$this->time(7);
        }else if(!empty($_GET['time'])){
            $arr=$this->time("time",$_GET['time']);
        }
        //根据时间去查询pv和uv
        $pvs=$this->table->pv_uv($arr);
        //这是所有pv量
        $data['all_pv']=$pvs['total'];

        //查询所有uv
        $uvs=$this->table->pv_uvs($arr);
        $data['all_uv']=count($uvs);
//        var_dump($data);exit;
            $chartOption['tooltip']['trigger'] = "axis";

            $chartOption['legend']['data'] = ['pv','uv'];

            $chartOption['toolbox']['show'] = "true";
//            $chartOption['toolbox']['feature']['dataView']['readOnly'] = "false";
//            $chartOption['toolbox']['feature']['magicType']['type'] = ['line', 'bar'];

            $chartOption['xAxis']['type'] = "category";
            $chartOption['xAxis']['boundaryGap'] = 'false';
            $chartOption['xAxis']['data'] = ["开始","累计"];

            $chartOption['yAxis']['type'] = 'value';
        $chartOption['yAxis']['axisLabel']['formatter'] = '{value}';

            $series = [
                ["name" => "pv", "type" => "line", "data" => [0,$data['all_pv']]],
                ["name" => "uv", "type" => "line", "data" => [0,$data['all_uv']]]
            ];
            $chartOption["series"] = $series;
            $data["option"] = json_encode($chartOption);
        $data['html_style']=1;
        if(!empty($_GET['date'])){
            $data['time']=$_GET['date'];
        }else{
            $data['time']="";
        }
        $this->layout("statics/index",$data);
    }

    /**
     * 用户分享数据
     */
    public function share(){
        $arr=[];
        if(!empty($_GET['date']) && $_GET['date']==1){
            $arr=$this->time(1);
        }else if(!empty($_GET['date']) && $_GET['date']==3){
            $arr=$this->time(3);
        }else if(!empty($_GET['date']) && $_GET['date']==7){
            $arr=$this->time(7);
        }else if(!empty($_GET['time'])){
            $arr=$this->time("time",$_GET['time']);
        }
        //根据时间去查询pv和uv
        $shares=$this->table->share($arr);
        $share=count($shares);
        $chartOption['tooltip']['trigger'] = "axis";

        $chartOption['legend']['data'] = ['pv','uv'];

        $chartOption['toolbox']['show'] = "true";
//        $chartOption['toolbox']['feature']['dataView']['readOnly'] = "false";
//        $chartOption['toolbox']['feature']['magicType']['type'] = ['line', 'bar'];

        $chartOption['xAxis']['type'] = "category";
        $chartOption['xAxis']['boundaryGap'] = 'false';
        $chartOption['xAxis']['data'] = ["开始","累计"];

        $chartOption['yAxis']['type'] = 'value';
        $chartOption['yAxis']['axisLabel']['formatter'] = '{value}';

        $series = [
            ["name" => "分享", "type" => "line", "data" => [0,$share]]
        ];
        $chartOption["series"] = $series;
        $data["option"] = json_encode($chartOption);
        $all_shares=$this->table->all_share();
        $all_share=count($all_shares);
        $data['all_share']=$all_share;
        $data['html_style']=2;
        if(!empty($_GET['date'])){
            $data['time']=$_GET['date'];
        }else{
            $data['time']="";
        }
        $this->layout("statics/share",$data);
    }

    /**
     * 点击排行
     */
    public function click(){
        //去原始素材表查询数据
        $arr=[];
        if(!empty($_GET['date']) && $_GET['date']==1){
            $arr=$this->time(1);
        }else if(!empty($_GET['date']) && $_GET['date']==3){
            $arr=$this->time(3);
        }else if(!empty($_GET['date']) && $_GET['date']==7){
            $arr=$this->time(7);
        }else if(!empty($_GET['time'])){
            $arr=$this->time("time",$_GET['time']);
        }
        //根据时间去查询pv和uv
        $click=$this->table->click_name();
        $names=[];
        $clicks=[];
        if(!empty($click)){
            foreach($click as $k=>$v){
//                if(mb_strlen($v['video_desc'])>13){
//                    $names[]=mb_substr($v['video_desc'],0,13);
//                }else{
//                    $names[]=$v['video_desc'];
//                }
                $names[]=$v['video_desc'];
                $click_detail=$this->table->click_detail($v['video_uuid'],$arr);
                if(empty($click_detail)){
                    $clicks[]=0;
                }else{
                    $clicks[]=count($click_detail);
                }
            }
        }
//        var_dump($names);exit;
        //如果视频名称大于10，就把视频名称截取10

        $chartOption['tooltip']['trigger'] = "axis";
        $chartOption['tooltip']['axisPointer']['type'] = "shadow";

        $chartOption['legend']['data'] = ['点击量'];

        $chartOption['xAxis']['type'] = 'value';
        $chartOption['xAxis']['boundaryGap'] = [0,0.01];

        $chartOption['yAxis']['type'] = 'category';

//        $chartOption['yAxis']['axisLabel']['interval'] = 'auto';
        $chartOption['yAxis']['data'] = $names;
        $chartOption['grid']['left'] = '30%';
        $chartOption['series']['name'] = '点击量';
        $chartOption['series']['type'] = 'bar';
        $chartOption['series']['data'] = $clicks;

        $data['option']=json_encode($chartOption);
        $data['html_style']=3;
        if(!empty($_GET['date'])){
            $data['time']=$_GET['date'];
        }else{
            $data['time']="";
        }
        $this->layout("statics/click",$data);
    }

    /**
     * 配音基础数据
     */
    public function dub_basics(){
        $arr=[];
        if(!empty($_GET['date']) && $_GET['date']==1){
            $arr=$this->time(1);
        }else if(!empty($_GET['date']) && $_GET['date']==3){
            $arr=$this->time(3);
        }else if(!empty($_GET['date']) && $_GET['date']==7){
            $arr=$this->time(7);
        }else if(!empty($_GET['time'])){
            $arr=$this->time("time",$_GET['time']);
        }
        $dub_basics=$this->table->dub_basics($arr);
        //所有pv
        $pv=count($dub_basics);

        //所有uv
        $dubs=$this->table->dubs($arr);
        $uv=count($dubs);
//        $uvs=[];
//        if(!empty($dub_basics)){
//            foreach($dub_basics as $k=>$v){
//                $uvs[]=$v['log_ip'];
//            }
//        }
//        $uv=array_unique($uvs);


        $chartOption['tooltip']['trigger'] = "axis";

        $chartOption['legend']['data'] = ['pv','uv'];

        $chartOption['toolbox']['show'] = "true";
//        $chartOption['toolbox']['feature']['dataView']['readOnly'] = "false";
//        $chartOption['toolbox']['feature']['magicType']['type'] = ['line', 'bar'];

        $chartOption['xAxis']['type'] = "category";
        $chartOption['xAxis']['boundaryGap'] = 'false';
        $chartOption['xAxis']['data'] = ["开始","累计"];

        $chartOption['yAxis']['type'] = 'value';
        $chartOption['yAxis']['axisLabel']['formatter'] = '{value}';

        $chartOption["series"] = [
            ["name" => "pv", "type" => "line", "data" => [0,$pv]],
            ["name" => "uv", "type" => "line", "data" => [0,$uv]]
        ];
        $data["option"] = json_encode($chartOption);
        $dub_basic=$this->table->all_dub_basics();
        $all_pvs=[];
        $all_uvs=[];
        if(!empty($dub_basic)){
            foreach($dub_basic as $kk=>$vv){
                $all_pvs[]=$vv['log_id'];
                $all_uvs[]=$vv['log_ip'];
            }
        }
        $all_pv=count($all_pvs);
        $all_uv=array_unique($all_uvs);
        $all_uv=count($all_uv);
        $data['all_pv']=$all_pv;
        $data['all_uv']=$all_uv;

        $data['html_style']=4;
        if(!empty($_GET['date'])){
            $data['time']=$_GET['date'];
        }else{
            $data['time']="";
        }
        $this->layout("statics/dub_basics",$data);
    }

    /**
     * 配音产出数据
     */
    public function dub_produce(){
        $arr=[];
        if(!empty($_GET['date']) && $_GET['date']==1){
            $arr['time']=$this->time(1);
        }else if(!empty($_GET['date']) && $_GET['date']==3){
            $arr['time']=$this->time(3);
        }else if(!empty($_GET['date']) && $_GET['date']==7){
            $arr['time']=$this->time(7);
        }else if(!empty($_GET['time'])){
            $arr['time']=$this->time("time",$_GET['time']);
        }else if(!empty($_GET['select_type'])){
            $arr['select_type']=$_GET['select_type'];
        }
        $dub_produces=$this->table->dub_produces($arr);
        $dub_produces_num=count($dub_produces);

        $chartOption['tooltip']['trigger'] = "axis";

        $chartOption['legend']['data'] = ['产出'];

        $chartOption['toolbox']['show'] = "true";
//        $chartOption['toolbox']['feature']['dataView']['readOnly'] = "false";
//        $chartOption['toolbox']['feature']['magicType']['type'] = ['line', 'bar'];

        $chartOption['xAxis']['type'] = "category";
        $chartOption['xAxis']['boundaryGap'] = 'false';
        $chartOption['xAxis']['data'] = ["开始","累计"];

        $chartOption['yAxis']['type'] = 'value';
        $chartOption['yAxis']['axisLabel']['formatter'] = '{value}';

        $chartOption["series"] = ["name" => "产出", "type" => "line", "data" => [0,$dub_produces_num]];
        $data["option"] = json_encode($chartOption);

        //查询所有产出
        $all_dub_produces=$this->table->all_dub_produces();
        $all_dub_produces_num=count($all_dub_produces);
        $data['all_dub_produces_num']=$all_dub_produces_num;

        $data['html_style']=5;
        if(!empty($_GET['date'])){
            $data['time']=$_GET['date'];
        }else{
            $data['time']="";
        }
        $this->layout("statics/dub_produce",$data);
    }

    /**
     * 配音产出比
     */
    public function output_ratio(){
        $arr=[];
        if(!empty($_GET['date']) && $_GET['date']==1){
            $arr['time']=$this->time(1);
        }else if(!empty($_GET['date']) && $_GET['date']==3){
            $arr['time']=$this->time(3);
        }else if(!empty($_GET['date']) && $_GET['date']==7){
            $arr['time']=$this->time(7);
        }else if(!empty($_GET['time'])){
            $arr['time']=$this->time("time",$_GET['time']);
        }else if(!empty($_GET['select_type'])){
            $arr['select_type']=$_GET['select_type'];
        }
        //先去查出30条配音视频
        $video=$this->table->click_name();
        $names=[];
        $dub=[];
        $produce=[];
        $proportion=[];
        if(!empty($video)){
            foreach($video as $k=>$v){
                $names[]=$v['video_desc'];
                //这是配音
                $pvs=$this->table->click_details($v['video_uuid'],$arr);
                $pv=count($pvs);
                if(empty($pvs)){
                    $dub[]=0;
                }else{
                    $dub[]=$pv;
                }
                //这是产出
                $produces=$this->table->produce($v['video_uuid'],$arr);
                $p=count($produces);
                if(empty($produces)){
                    $produce[]=0;
                }else{
                    $produce[]=$p;
                }
                //这是产出比
                if($p==0 || $pv==0){
                    $proportion[]=0;
                }else{
                    $proportion[]=round($p/$pv,2);
//                    $proportion[]=$p/$pv;
                }
            }
        }

        $chartOption['tooltip']['trigger'] = "axis";
        $chartOption['tooltip']['axisPointer']['type'] = "shadow";

        $chartOption['legend']['data'] = ['配音','产出',"产出比"];

        $chartOption['yAxis']['type'] = "category";
        $chartOption['yAxis']['data'] = $names;
        $chartOption['grid']['left'] = '30%';
        $chartOption['yAxis']['axisTick']['alignWithLabel'] ="true";

        $chartOption['xAxis']['type'] = 'value';

        $chartOption["series"] = [
            ["name" => "配音", "type" => "bar", "data" => $dub],
            ["name" => "产出", "type" => "bar", "data" => $produce],
            ["name" => "产出比", "type" => "line","data" => $proportion]
        ];
        $data["option"] = json_encode($chartOption);

        $data['html_style']=6;
        if(!empty($_GET['date'])){
            $data['time']=$_GET['date'];
        }else{
            $data['time']="";
        }
        $this->layout("statics/output_ratio",$data);
    }

    /**
     * 格式化时间
     */
    public function time($date=1,$time=""){
        $today = strtotime(date("Y-m-d"),time());
        $todays=date("Y-m-d H:i:s",$today);
        if($date==1){
           $dates['start']=date("Y-m-d H:i:s",$today-86400);
            $dates['end']=$todays;
        }elseif($date==3){
            $dates['start']=date("Y-m-d H:i:s",$today-3*86400);
            $dates['end']=$todays;
        }elseif($date==7){
            $dates['start']=date("Y-m-d H:i:s",$today-7*86400);
            $dates['end']=$todays;
        }elseif($date=="time"){
            $arr=explode("~",$time);
            $start=strtotime($arr[0],time());
            $end=strtotime($arr[1],time());
            $dates['start']=date("Y-m-d H:i:s",$start);
            $dates['end']=date("Y-m-d H:i:s",$end+86400);
        }
        return $dates;
    }

    /**
     * 推送视频到新媒资
     */
    public function push(){
        $array = $this->table->push(284);
        echo json_encode($array,JSON_UNESCAPED_UNICODE);
    }
}